/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package outils;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JTable;
import javax.swing.table.TableModel;

/**
 *
 * @author Aude Legrand, Alexandre Becquet
 */
public class ExcelExporter {
    private File fichierExcel;

    public ExcelExporter(String nom) {
        fichierExcel = new File(nom);
    }
    
    public void exportTable(JTable table) throws IOException {
        TableModel model = table.getModel();
        
        FileWriter out = new FileWriter(this.fichierExcel);

        for(int i=0; i < model.getColumnCount();i++) {
            out.write(model.getColumnName(i)+"\t");
        }
        out.write("\n");

        for(int i=0; i < model.getRowCount();i++){
            for(int j=0;j < model.getColumnCount();j++){
                out.write(model.getValueAt(i,j)+"" + "\t");
            }
            out.write("\n");
        }
        out.close();
        System.out.println("write to " + this.fichierExcel);
    }

    public void openTable() throws IOException {
        Runtime run = Runtime.getRuntime();
        String lcOSName = System.getProperty("os.name").toLowerCase();
        boolean MAC_OS_X = lcOSName.startsWith("mac os x");
        if(MAC_OS_X){
            run.exec("open "+ this.fichierExcel);
        } else {
            run.exec("cmd.exe /c start " + this.fichierExcel);
        }
        System.out.println(this.fichierExcel + " opened");
    }
}

